/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.core;

import java.util.ArrayList;
import java.util.List;

import com.ewerp.mud.core.exceptions.EwerpMudException;
import com.ewerp.mud.core.logging.ILog;
import com.ewerp.mud.core.plugin.EwerpPluginManager;
import com.ewerp.mud.lifecycle.ILifecycleListener;
import com.ewerp.mud.lifecycle.ILifecycleProvider;
import com.ewerp.mud.plugin.IPluginManager;


public class EwerpMud implements ILifecycleProvider, Runnable {
    
    protected static final EwerpMud instance = new EwerpMud();
    public static final String defaultNamespace = "EwerpDefault";
    public static String[] commandLineArgs = null;
    
    public static final Object shutdown = new Object();
    
    protected IPluginManager pluginManager = null;
    protected final List<ILifecycleListener> lifecycleListeners = new ArrayList<ILifecycleListener>();
    protected ILog log;
    
    protected enum LifecycleState {INIT, RUN, STOP};
    
    @Override
    public void run() {
        try {
            // Setup the plugin manager
            pluginManager.setLifecycleProvider(this);
            pluginManager.init();
            
            // Initialize plugins
            notifyLifecycle(LifecycleState.INIT);
            
            // Get the logger
            log = (ILog)pluginManager.getPlugin(defaultNamespace, ILog.class);
            log.logInfo("Starting the Ewerp Mud server...");
            
            notifyLifecycle(LifecycleState.RUN);

            waitForShutdownSignal();
            
            notifyLifecycle(LifecycleState.STOP);

            log.logInfo("The EwerpMud server shutdown complete");
        } catch (EwerpMudException e) {
            log.logError("An error has occurred", e);
        }
    }
    
    public static void signalShutdown() {
        instance.notifyShutdown();
    }
    
    public void notifyShutdown() {
        synchronized(shutdown) {
            shutdown.notifyAll();
        }
    }
    
    protected void waitForShutdownSignal() {
        synchronized(shutdown) {
            try {
                shutdown.wait();
            } catch (InterruptedException e) {
            }
        }
    }
    
    public static void main(String[] args) {
    	commandLineArgs = args;
        instance.setPluginManager(EwerpPluginManager.getInstance());
        instance.run();
    }
    
    public void setPluginManager(IPluginManager pluginManager) {
        this.pluginManager = pluginManager;
    }
    
    public IPluginManager getPluginManager() {
        return pluginManager;
    }

    @Override
    public void registerListener(ILifecycleListener listener) {
        lifecycleListeners.add(listener);
    }

    @Override
    public void unregisterListener(ILifecycleListener listener) {
        lifecycleListeners.remove(listener);
    }
    
    protected void notifyLifecycle(LifecycleState lifecycleState) {
        switch(lifecycleState) {
            case INIT: {
                for(ILifecycleListener listener : lifecycleListeners) {
                    listener.init();
                }
            } break;
            case RUN: {
                for(ILifecycleListener listener : lifecycleListeners) {
                    listener.start();
                }
            } break;
            case STOP: {
                for(ILifecycleListener listener : lifecycleListeners) {
                    listener.stop();
                }
            } break;
        }
    }
}
